package com.monkeygroup.mygoplus.view.home.activity.person;

import android.app.Activity;
import android.content.Intent;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.monkeygroup.mygoplus.R;
import com.monkeygroup.mygoplus.base.BaseActivity;
import com.monkeygroup.mygoplus.common.utils.EncryptUtil;
import com.monkeygroup.mygoplus.common.utils.LocalInfo;
import com.monkeygroup.mygoplus.common.utils.VilidateUtil;
import com.monkeygroup.mygoplus.http.PersonHttpModel;
import com.monkeygroup.mygoplus.http.RequestUrl;
import com.monkeygroup.mygoplus.user.vo.PersonalNomalInfo;

import java.util.HashMap;
import java.util.Map;

/**
 * 綁定賬號
 * Created by cumt on 2017/6/20.
 */

public class PersonBindAccountActivity extends BaseActivity implements View.OnClickListener {


    public static void gotoActivity(Activity activity) {
        Intent intent = new Intent(activity,
                PersonBindAccountActivity.class);

        activity.startActivityForResult(intent,101);
    }

    private Activity activity;
    private ImageView imgBack;
    private TextView txtTitle;

    private EditText et_web_account,et_web_pwd;
    private TextView txt_bind_account_ok;

    //請求參數
    private Map<String, String> params;
    private PersonHttpModel httpModel,httpLoginModel;

    @Override
    public void onClick(View v) {

    }

    @Override
    public void setContentView() {
        setContentView(R.layout.person_bind_account);
    }

    @Override
    public void initView() {

        activity = PersonBindAccountActivity.this;
        imgBack = (ImageView) findViewById(R.id.img_back);
        txtTitle = (TextView) findViewById(R.id.txt_title);
        txtTitle.setText("綁定Web資料");

        et_web_account = (EditText)findViewById(R.id.et_web_account);
        et_web_pwd= (EditText)findViewById(R.id.et_web_pwd);

        txt_bind_account_ok= (TextView)findViewById(R.id.txt_bind_account_ok);

        PersonalNomalInfo user = LocalInfo.getUserInfo(PersonBindAccountActivity.this);
        params = new HashMap<>();
        params.put("appacc",user.getMobile());

        initWatcher();

        initRequest();

    }

    @Override
    public void setListener() {

    }

    @Override
    public void doEvent() {

        imgBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        txt_bind_account_ok.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                boolean isread = txt_bind_account_ok.getBackground().getCurrent().getConstantState().equals(getResources().getDrawable(R.drawable.btn_red_shape).getCurrent().getConstantState());
                if(isread){

                    httpLoginModel.requestUserLogin();
                }
            }
        });

    }

    private void initWatcher() {


        TextWatcher aname = new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                // params.put("name", String.valueOf(et_web_pwd.getText()));

            }

            @Override
            public void afterTextChanged(Editable s) {

                if (VilidateUtil.isAccount(String.valueOf(et_web_account.getText()))) {

                    params.put("account",String.valueOf(et_web_account.getText()));
                    params.put("acc",String.valueOf(et_web_account.getText()));
                    if(VilidateUtil.isPwd(String.valueOf(et_web_pwd.getText()))){
                        txt_bind_account_ok.setBackgroundResource(0);
                        txt_bind_account_ok.setBackgroundResource(R.drawable.btn_red_shape);
                    }
                    else{
                        txt_bind_account_ok.setBackgroundResource(0);
                        txt_bind_account_ok.setBackgroundResource(R.drawable.btn_grey_shape);
                    }

                }
                else{
                    txt_bind_account_ok.setBackgroundResource(0);
                    txt_bind_account_ok.setBackgroundResource(R.drawable.btn_grey_shape);
                }

            }
        };

        et_web_account.addTextChangedListener(aname);


        TextWatcher cname = new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                // params.put("name", String.valueOf(et_web_pwd.getText()));

            }

            @Override
            public void afterTextChanged(Editable s) {

                if (VilidateUtil.isPwd(String.valueOf(et_web_pwd.getText()))) {
                    params.put("passwd", EncryptUtil.encrypt(String.valueOf(et_web_pwd.getText())));
                    if(VilidateUtil.isAccount(String.valueOf(et_web_account.getText()))){
                        txt_bind_account_ok.setBackgroundResource(0);
                        txt_bind_account_ok.setBackgroundResource(R.drawable.btn_red_shape);

                    }
                    else{
                        txt_bind_account_ok.setBackgroundResource(0);
                        txt_bind_account_ok.setBackgroundResource(R.drawable.btn_grey_shape);
                    }
                }
                else{
                    txt_bind_account_ok.setBackgroundResource(0);
                    txt_bind_account_ok.setBackgroundResource(R.drawable.btn_grey_shape);
                }
            }
        };

        et_web_pwd.addTextChangedListener(cname);

    }

    private void initRequest() {


        httpLoginModel = new PersonHttpModel(RequestUrl.appLoginUrl, params, new PersonHttpModel.SimpleOnHttpRequestListener() {

            @Override
            public void onRequestFinished(String resultCode, Integer timestamp) {

            }

            @Override
            public void onRequestFinished(String resultCode, int seq, int isbind, String acc) {


            }

            @Override
            public void onRequestFinished(String resultCode, int seq, int flag, String flag_name, int secflag, String secflag_name) {
                //ERROR_OK
                if("ERROR_OK".equals(resultCode)){
                    Toast.makeText(activity, "賬號密碼正確",
                            Toast.LENGTH_SHORT).show();
                    httpModel.requestUserInfo();
                }

                else{
                    Toast.makeText(activity, "賬號或密碼不對",
                            Toast.LENGTH_SHORT).show();
                }
            }
        }, true);


        httpModel = new PersonHttpModel(RequestUrl.accbind, params, new PersonHttpModel.SimpleOnHttpRequestListener() {

            @Override
            public void onRequestFinished(String resultCode, Integer timestamp) {

            }

            @Override
            public void onRequestFinished(String resultCode, int seq, int isbind, String acc) {

                if("ERROR_OK".equals(resultCode)){
                    Toast.makeText(activity, seq+"綁定成功",
                            Toast.LENGTH_SHORT).show();
                    final Intent intent=new Intent();
                    PersonBindAccountActivity.this.setResult(RESULT_OK,intent);
                    finish();
                }
                else{
                    Toast.makeText(activity, seq+"綁定失敗",
                            Toast.LENGTH_SHORT).show();
                }


            }

            @Override
            public void onRequestFinished(String resultCode, int seq, int flag, String flag_name, int secflag, String secflag_name) {

            }
        }, true);

    }


}
